<template>
  <!-- 维护作业-核保等级维护页面 -->
  <div class="fillcontain">
    <el-form :model="searchParams" ref="ruleForm" label-width="100px" class="search_container">
      <el-row>
        <el-col :span="6">
          <el-form-item label="核保等级">
            <el-select v-model="searchParams.uwlevel">
              <el-option v-for="item in uwlevelList" :key="item.eutblitem" :label="item.uwlevel_s"
                         :value="item.eutblitem">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" class="text_center">
          <el-button type="primary" icon="el-icon-search" @click="subGetList"></el-button>
          <el-button type="primary" plain icon="el-icon-refresh" @click="resetSearch"></el-button>
        </el-col>
      </el-row>
    </el-form>
    <div class="contain">
      <el-row class="bgfff pb15">
        <el-col :span="24" class="pt5">
          <el-button type="danger" @click="undGradeAddGo()" plain>新增</el-button>
        </el-col>
      </el-row>
      <!-- 查询结果 -->
      <div class="table_container">
        <el-table v-loading="loading" :data="undGradetableData" highlight-current-row
                  header-cell-class-name="table-header-class"
                  style="width:100%">
          <el-table-column min-width="120" property="uwlevel" label="核保等级" align='center' show-overflow-tooltip>
          </el-table-column>
          <el-table-column min-width="120" property="uwtype" label="核保类别" align='center' show-overflow-tooltip>
          </el-table-column>
          <el-table-column property="gramt" label="授权额度" align='center' show-overflow-tooltip>
          </el-table-column>
          <el-table-column min-width="120" property="agcomrt" label="加费频点" align='center' show-overflow-tooltip>
          </el-table-column>
          <el-table-column min-width="120" property="flag01" label="是否使用二级全选" align='center' show-overflow-tooltip>
          </el-table-column>
          <el-table-column min-width="120" property="flag02" label="单位" align='center' show-overflow-tooltip>
          </el-table-column>
          <el-table-column min-width="120" fixed="right" label="操作" align='center'>
            <template slot-scope="scope">
              <!-- <el-button type="text" size="small" @click="undGradeUpdateGo(scope.row)">修改</el-button> -->
              <span class="operationIcon" @click="undGradeUpdateGo(scope.row)">
                <el-tooltip class="item" effect="light" content="修改" placement="top">
                  <i class="el-icon-edit"></i>
                </el-tooltip>
              </span>

              <!-- <el-button type="text" size="small" @click="deleteGradeInfo(scope.row)">删除</el-button>             -->
              <span class="operationIcon ml5" @click="deleteGradeInfo(scope.row)">
                <el-tooltip class="item" effect="light" content="删除" placement="top">
                  <i class="el-icon-delete"></i>
                </el-tooltip>
              </span>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col :span="24">
            <div class="pagination">
              <el-pagination background v-if='paginations.total > 0' :page-sizes="paginations.pageSizes"
                             :page-size="paginations.pageSize"
                             :layout="paginations.layout" :total="paginations.total"
                             :current-page='paginations.pageIndex'
                             @current-change='handleCurrentChange' @size-change='handleSizeChange'>
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- 弹框 修改-->
    <el-dialog title="核保等级" :visible.sync="undGradeDialog" width="60%" center>
      <el-form :model="undGradeDetail" :rules="oneRules" ref="oneForm" label-width="120px">
        <el-row>
          <el-col :span="12" class="common-box">
            <el-form-item label="核保等级" prop="uwlevel">
              <el-select v-if="update" disabled v-model="undGradeDetail.uwlevel" placeholder="">
                <el-option v-for="item in uwlevelList" :key="item.eutblitem" :label="item.uwlevel_s"
                           :value="item.eutblitem">
                </el-option>
              </el-select>
              <el-select v-else v-model="undGradeDetail.uwlevel" placeholder="">
                <el-option v-for="item in uwlevelList" :key="item.eutblitem" :label="item.uwlevel_s"
                           :value="item.eutblitem">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="核保类别" prop="uwtype">
              <el-select v-if="update" disabled v-model="undGradeDetail.uwtype" placeholder="">
                <el-option v-for="item in uwtypeList" :key="item.eutblitem" :label="item.uwtype_s"
                           :value="item.eutblitem">
                </el-option>
              </el-select>
              <el-select v-else v-model="undGradeDetail.uwtype" placeholder="">
                <el-option v-for="item in uwtypeList" :key="item.eutblitem" :label="item.uwtype_s"
                           :value="item.eutblitem">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="授权额度" prop="gramt">
              <el-input clearable placeholder="" v-model="undGradeDetail.gramt"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="加费频点" prop="agcomrt">
              <el-input clearable placeholder="" v-model="undGradeDetail.agcomrt"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="是否使用二级权限" prop="flag01">
              <el-select v-model="undGradeDetail.flag01" placeholder="">
                <el-option value="Y" label="Y"></el-option>
                <el-option value="N" label="N"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="common-box">
            <el-form-item label="单位" prop="flag02">
              <el-select v-model="undGradeDetail.flag02" placeholder="">
                <el-option value="Y" label="Y"></el-option>
                <el-option value="N" label="N"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="saveGradeInfo" class="Defaultbutton">保 存</el-button>
        <el-button @click="undGradeDialog = false">返 回</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import {mapState, mapGetters} from 'vuex'
  import constant from '@/utils/constant';
  import {getNucleusList, initSelect, deleteEuffpky, updateEuffpky} from '@/api/setting/maintain/underwrite.js';
  export default {
    data() {
      var checkGramt = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('授权额度不能为空!'));
        }
          if (value!=0 && !Number(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value > 9999999999999.99) {
              callback(new Error('授权额度的绝对值只能等于或小于9999999999999.99'));
            } else {
              callback();
            }
          }
      };
      var checkAgcomrt = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('加费频点不能为空!'));
        }
          if (value!=0 && !Number(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value > 999.99) {
              callback(new Error('加费频点的绝对值只能等于或小于999.99'));
            } else {
              callback();
            }
          }
      };
      return {
        undGradeDialog: false,//默认隐藏
        // 查询条件
        searchParams: {
          uwlevel: '',
        },
        undGradetableData: [//数据
        ],
        uwlevelList: [],
        uwtypeList: [],
        loading: false,
        update: false,
        option: '',
        undGradeDetail: {
          uwlevel: '', // 核保等级
          uwtype: '', // 核保类别
          gramt: '', // 授权额度
          agcomrt: '', // 加费频点
          flag01: '', // 是否使用二级权限
          flag02: '' // 单位
        },
        oneRules: {
          uwlevel: [
            {required: true, message: '核保等级不能为空!', trigger: 'change'}
          ],
          uwtype: [
            {required: true, message: '核保类别不能为空!', trigger: 'change'}
          ],
          gramt: [
            {validator: checkGramt, trigger: 'blur'}
          ],
          agcomrt: [
            {validator: checkAgcomrt, trigger: 'blur'}
          ],
          flag01: [],
          flag02: []
        },
        //需要给分页组件传的信息
        paginations: {
          total: 0,        // 总数
          pageIndex: 1,  // 当前位于哪页
          pageSize: 10,   // 1页显示多少条
          pageSizes: [5, 10, 15, 20],  //每页显示多少条
          layout: "total, sizes, prev, pager, next, jumper"   // 翻页属性
        },
        pickerOptions1: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          }
        }
      }
    },
    created() {
      initSelect().then(res => {
        if (res.resultCode == constant.SUCCESS && res.data) {
          this.uwlevelList = res.data.uwlevelList;
          this.uwtypeList = res.data.uwtypeList;
        }
      }).catch(err => {
        this.$emit('checkData', false)
      })
    },
    mounted() {
      // this.getList();
    },
    methods: {
      //新增核保等级
      undGradeAddGo() {
        this.$set(this.undGradeDetail, 'uwlevel', '');
        this.$set(this.undGradeDetail, 'uwtype', '');
        this.$set(this.undGradeDetail, 'agcomrt', '');
        this.$set(this.undGradeDetail, 'gramt', '0.00');
        this.$set(this.undGradeDetail, 'flag01', 'N');
        this.$set(this.undGradeDetail, 'flag02', 'N');
        this.undGradeDialog = true;
        this.update = false;
      },
      //更新核保等级
      undGradeUpdateGo(row) {
        this.undGradeDialog = true;
        this.undGradeDetail = row;
        this.update = true;
      },
      //保存信息
      saveGradeInfo() {
        this.$refs['oneForm'].validate((valid) => {
          if (valid) {
            if (this.update) {
              this.undGradeDetail.method = "modify";
            } else {
              this.undGradeDetail.method = "add";
            }
            this.$store.dispatch('loading/CHNAGE_SUBLOADING', true)
            updateEuffpky(this.undGradeDetail).then(res => {
              this.$store.dispatch('loading/CHNAGE_SUBLOADING', false)
              if (res.resultCode == constant.SUCCESS) {
                this.popMsg("保存成功！","success")
                this.searchParams.uwlevel = this.undGradeDetail.uwlevel
                this.undGradeDialog = false;
                this.getList();
              }
            }).catch(err => {
              this.$store.dispatch('loading/CHNAGE_SUBLOADING', false)
            });
          }
        });
      },
      //删除核保等级
      deleteGradeInfo(item) {
        this.$confirm("删除！资料可能已被使用，若删除可能造成资料错误~，请问是否确定删除？", '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {

          let param = {
            "uwlevel": item.uwlevel,
            "uwtype": item.uwtype,
          }
          this.$store.dispatch('loading/CHANGE_LOADING', true)
          deleteEuffpky(param).then(res => {
            this.$store.dispatch('loading/CHANGE_LOADING', false)
            if (res.resultCode == constant.SUCCESS) {
              this.popMsg("删除成功！","success")
              this.getList();
            }

          }).catch(err => {
            this.$store.dispatch('loading/CHANGE_LOADING', false);
          })
        }).catch(() => {

        });

      },

      // 查询列表
      getList() {
        let para = {
          "uwlevel": this.searchParams.uwlevel,
          "pageSize": this.paginations.pageSize,
          "pageNumber": this.paginations.pageIndex,
        }
        this.$store.dispatch('loading/CHANGE_LOADING', true)
        getNucleusList(para).then(res => {
          this.$store.dispatch('loading/CHANGE_LOADING', false)
          if (res.resultCode == constant.SUCCESS && res.data) {
            if (res.data.list.length > 0) {
              this.paginations.total = res.data.total;
              this.undGradetableData = res.data.list;
            } else {
              this.popMsg("无符合条件资料!", "error");
              this.paginations.total = 0;
              this.undGradetableData = [];
            }


          }
        }).catch(err => {
          this.$store.dispatch('loading/CHANGE_LOADING', false);
        })
      },
      // 每页多少条切换
      handleSizeChange(pageSize) {
        this.paginations.pageSize = pageSize;
        this.getList();
      },
      // 上下分页
      handleCurrentChange(page) {
        this.paginations.pageIndex = page;
        this.getList();
      },
      // 根据条件查询列表
      subGetList() {
        this.paginations.pageIndex = 1;
        this.getList();
      },
      // 清除查询条件
      resetSearch() {
        this.searchParams.uwlevel = '';
      },
      popMsg(message, type) {
        this.$message({
          message: message,
          type: type
        });
      },
    },
    computed: {
      ...mapGetters(['tags']),
      reload() {
        return this.$store.state.loading.reload;
      }
    },
    activated() {
      if (this.reload) {
        this.$store.dispatch('loading/CHNAGE_RELOAD', false)
      }
    }
  }

</script>

<style lang="less" scoped>
  .fillcontain {
    padding-bottom: 0;
  }

  .contain {
    background: #fff;
    padding: 10px;
    margin-bottom: 20px;
  }

  .pagination {
    padding: 10px 20px;
    text-align: right;
  }

  .operationIcon {
    // padding: 12px;
    border: 1px solid #e5e5e5;
    border-radius: 50%;
    width: 22px;
    height: 22px;
    display: inline-block;
    cursor: pointer;
    box-shadow: 0 0 3px #f9f9f9;
  }

  .search_container {
    padding: 20px 20px 0 20px; //    border-top: 1px solid #e1e1e1;
  }

  .common-box {
    padding-bottom: 15px;
  }

  .common-lab {
    line-height: normal !important;
    text-align: right;

    .labelName {
      text-align: right;
    }
  }
</style>